//
//  DataManager.swift
//  OneDollar
//
//  Created by leeqb on 11/28/15.
//  Copyright © 2015 powerlee. All rights reserved.
//

import UIKit

let userInfoKey = "KeyUserInfo"
let cityListKey = "KeyCityList"

class DataManager: NSObject {
    
    // 获取用户信息
    class func userInfo() -> [NSObject : AnyObject]? {
        let userDefaults = NSUserDefaults.standardUserDefaults()
        return userDefaults.objectForKey(userInfoKey) as? [NSObject : AnyObject]
    }
    
    // 设置用户信息
    class func setUserInfo(userInfo: [NSObject : AnyObject]) {
        let userDefaults = NSUserDefaults.standardUserDefaults()
        userDefaults.setObject(userInfo, forKey: userInfoKey)
        userDefaults.synchronize()
    }
    
    /*
    * 更新本地用户数据
    * address = "";
    * birthday = "";
    * isMarried = "";
    * nickName = "";
    * photo = "http://mymost.gotoip55.com/service/img?img=null";
    * sex = "<null>";
    * sign = "";
    * trueName = "";
    * userId = 26;
    * userTel = 18602335806;
    */
    class func updateLocalUserInfo(key: String, value: String) {
        let userDefaults = NSUserDefaults.standardUserDefaults()
        var userInfo = userDefaults.objectForKey(userInfoKey) as? [NSObject : AnyObject]
        userInfo?[key] = value
        userDefaults.setObject(userInfo, forKey: userInfoKey)
        userDefaults.synchronize()
    }
    
    // 刷新用户信息
    class func refreshUserInfo(userId: NSInteger, success: (() -> Void), failed: (() -> Void)) {
        let params = ["userId": userId]
        AJNetworkHelper.instance.post("aj_user_Info", parameters: params, success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in
            if (responseObject["returnCode"] as? String == RetCodeSuccess){
                DataManager.setUserInfo(responseObject["result"] as! [NSObject : AnyObject])
                success()
            } else {
                failed()
            }
            }, failed: { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
                failed()
            }) { () -> Void in
        }
    }
    
    // 清除用户信息
    class func clearUserInfo() {
        let userDefaults = NSUserDefaults.standardUserDefaults()
        userDefaults.removeObjectForKey(userInfoKey)
        userDefaults.synchronize()
    }
    
    /*
    * 将城市数据缓存到本地
    * JSON数据格式：
    *   citylist:{"p":"省份", "c":[{"城市列表"...}]}
    */
    class func initCityInfo() {
        let userDefaults = NSUserDefaults.standardUserDefaults()
        let localCitys = userDefaults.objectForKey(cityListKey)
        if localCitys == nil {
            let filePath = NSBundle.mainBundle().pathForResource("city", ofType: "json") ?? ""
            if !filePath.isEmpty {
                do {
                    let cityData = NSData(contentsOfFile: filePath)
                    if cityData?.length > 0 {
                        let json = try NSJSONSerialization.JSONObjectWithData(cityData!, options: [])
                        userDefaults.setObject(json["citylist"], forKey: cityListKey)
                        userDefaults.synchronize()
                    }
                } catch {
                }
            }
        }
    }
    
}
